﻿// ***********************************************************************
// Assembly         : NIMServerApi
// Author           : Administrator
// Created          : 11-15-2016
//
// Last Modified By : Walter
// Last Modified On : 11-15-2016
// ***********************************************************************
// <copyright file="FriendMethod.cs" company="51iface">
//     Copyright 51iface©  2016
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using NIMServerApi.Commons;
using NIMServerApi.Messages;

namespace NIMServerApi.Methods
{
    /// <summary>
    /// 用户关系托管类.
    /// </summary>
    /// <seealso cref="NIMServerApi.Methods.BaseMethod" />
    public class FriendMethod:BaseMethod
    {
        public FriendMethod(string appKey, string appSecret):base(appKey,appSecret)
        {

        }

        /// <summary>
        /// 加好友.
        /// </summary>
        /// <param name="accid">加好友发起者accid.</param>
        /// <param name="faccid">加好友接收者accid.</param>
        /// <param name="type">The type.</param>
        /// <param name="msg">加好友对应的请求消息，第三方组装，最长256字符.</param>
        /// <returns>System.Int32.</returns>
        public int Add(string accid, string faccid, AnswFriendRequestType type,string msg)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            if (string.IsNullOrEmpty(faccid))
            {
                throw new ArgumentNullException("Paramer 'faccid' is required.");
            }
            string postStr = $"accid={accid}&faccid={faccid}&type={(int)type}";
            if (!string.IsNullOrEmpty(msg))
            {
                postStr += $"&msg={msg}";
            }
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/friend/add.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return base.TryGetJsonValue<int>(json, "code");
        }

        /// <summary>
        /// 更新好友相关信息，如加备注名，必须是好友才可以.
        /// </summary>
        /// <param name="accid">发起者accid.</param>
        /// <param name="faccid">要修改朋友的accid.</param>
        /// <param name="alias">给好友增加备注名.</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Paramer 'accid' is required.
        /// or
        /// Paramer 'faccid' is required.
        /// or
        /// Paramer 'alias' is required.
        /// </exception>
        public int Update(string accid, string faccid, string alias)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            if (string.IsNullOrEmpty(faccid))
            {
                throw new ArgumentNullException("Paramer 'faccid' is required.");
            }
            if (string.IsNullOrEmpty(alias))
            {
                throw new ArgumentNullException("Paramer 'alias' is required.");
            }
            string postStr = $"accid={accid}&faccid={faccid}&alias={alias}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/friend/update.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return base.TryGetJsonValue<int>(json, "code");
        }

        /// <summary>
        /// 删除好友关系.
        /// </summary>
        /// <param name="accid">The accid.</param>
        /// <param name="faccid">The faccid.</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Paramer 'accid' is required.
        /// or
        /// Paramer 'faccid' is required.
        /// </exception>
        public int Delete(string accid, string faccid)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            if (string.IsNullOrEmpty(faccid))
            {
                throw new ArgumentNullException("Paramer 'faccid' is required.");
            }
            string postStr = $"accid={accid}&faccid={faccid}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/friend/delete.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return base.TryGetJsonValue<int>(json, "code");
        }

        /// <summary>
        /// 查询某时间点起到现在有更新的双向好友.
        /// </summary>
        /// <param name="accid">The accid.</param>
        /// <param name="createtime">The createtime.</param>
        /// <returns>FriendMessage.</returns>
        public FriendMessage Get(string accid, string createtime= "1443599631111")
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            string postStr = $"accid={accid}&createtime={createtime}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/friend/get.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return json.JsonToObject<FriendMessage>();
        }

        /// <summary>
        /// 拉黑/取消拉黑；设置静音/取消静音.
        /// </summary>
        /// <param name="accid">用户帐号，最大长度32字符，必须保证一个APP内唯一.</param>
        /// <param name="targetAcc">被加黑或加静音的帐号.</param>
        /// <param name="relationType">本次操作的关系类型,1:黑名单操作，2:静音列表操作.</param>
        /// <param name="value">操作值，0:取消黑名单或静音，1:加入黑名单或静音.</param>
        /// <returns>System.Int32.</returns>
        public int SetSpecialRelation(string accid, string targetAcc, int relationType, int value)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            if (string.IsNullOrEmpty(targetAcc))
            {
                throw new ArgumentNullException("Paramer 'targetAcc' is required.");
            }
            string postStr = $"accid={accid}&targetAcc={targetAcc}&relationType={relationType}&value={value}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/user/setSpecialRelation.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return base.TryGetJsonValue<int>(json, "code");
        }

        /// <summary>
        /// 查看用户的黑名单和静音列表.
        /// </summary>
        /// <param name="accid">The accid.</param>
        /// <returns>BlackAndMuteListMessage.</returns>
        /// <exception cref="System.ArgumentNullException">Paramer 'accid' is required.</exception>
        public BlackAndMuteListMessage ListBlackAndMuteList(string accid)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            string postStr = $"accid={accid}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/user/listBlackAndMuteList.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return json.JsonToObject<BlackAndMuteListMessage>();
        }


    }
}